using Serilog;

namespace YAPACS.RisServer;

public class Program
{
    public async static Task<int> Main(string[] args)
    {
#if XPO_PROFILER
        DevExpress.Xpo.Logger.LogManager.SetTransport(new DevExpress.Xpo.Logger.Transport.NamedPipeLogger("RIS"));
#endif

        var configuration = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .Build();

        Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(configuration)
            .CreateLogger();

        try
        {
            var builder = WebApplication.CreateBuilder(args);
            builder.Host.AddAppSettingsSecretsJson()
                .UseAutofac()
                .UseSerilog();

            await builder.AddApplicationAsync<RisServerAbpModule>();
            var app = builder.Build();
            await app.InitializeApplicationAsync();

            Log.Information("Starting YAPACS.RisServer.");
            await app.RunAsync();
            return 0;
        }
        catch (Exception ex)
        {
            if (ex is HostAbortedException)
            {
                throw;
            }

            Log.Fatal(ex, "YAPACS.RisServer terminated unexpectedly!");
            return 1;
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }
}
